#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sched.h>
#include <ctype.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/if.h>

#include "public.h"

#define MAXLINE 128
#define FILE_DEV "/proc/scsi/scsi"
#define IFNAMSIZE 16

int b_quit = 0;

/* U-disk device obtain */
static int find_device()
{
  char buffer[MAXLINE] = {0};
  FILE * fp;
  char * p = NULL;

  if ((fp = popen("fdisk -l", "r")) == NULL) {
    ac_traces(MSG_LEVEL_ERROR, "popen() error!");
    return -1;
  }

  while (fgets(buffer, sizeof buffer, fp));
  ac_traces(MSG_LEVEL_DEBUG, "buffer=%s\n", buffer);
  if ('\n' == buffer[strlen(buffer) - 1]) {
    buffer[strlen(buffer) - 1] = '\0';
  }
  ac_traces(MSG_LEVEL_DEBUG, "[buffer=%s]\n", buffer);

  p = strstr(buffer, "sd");
  if (p != NULL) {
    ac_traces(MSG_LEVEL_DEBUG, "p=%s\n", p);
    pclose(fp);
    return 1;
  }

  pclose(fp);

  return 0;
}

static void xy_udisk_device_led(void)
{
  if (find_device()) {
    xy_red_bright();
  }
  else {
    xy_red_extinguish();
    ac_traces(MSG_LEVEL_INFO, "U-disk device not found");
  }
}

void xy_udisk_rw_led(void)
{
  /* no inplement */
}

void * xy_udisk_thread(void)
{
  while (b_quit == 0) {
    xy_udisk_device_led();
    sleep(1);
  }
}

int xy_udisk_thread_run(void)
{
  pthread_attr_t attr;
  pthread_t udisk_id = 0;
  int ret = 0;
  //void * ptr;

  pthread_attr_init(&attr);

  if (0 != (ret = pthread_create(&udisk_id, &attr, (void * (*)(void *))xy_udisk_thread, NULL))) {
    pr_err("pthread fail\n");
    ac_traces(MSG_LEVEL_ERROR, "create udisk thread fail. error=%d", ret);
    return false;
  }
  
  ac_traces(MSG_LEVEL_INFO, "u-disk thread create successfuly!");

  //pthread_join(udisk_id, &ptr);

  return true;
}

/* network availability obtain */
static int xy_network_obtain(char * eth, char * ipaddr)
{
  int sock_fd;
  struct sockaddr_in addr;
  struct ifreq ifr;

  /* get socket file descriptor */
  if ((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
    ac_traces(MSG_LEVEL_ERROR, "socket create fail.");
    return -1;
  }

  /* get ip address */
  strncpy(ifr.ifr_name, eth, IFNAMSIZE);
  ifr.ifr_name[IFNAMSIZE - 1] = '\0';

  if (ioctl(sock_fd, SIOCGIFADDR, &ifr) < 0) {
    ac_traces(MSG_LEVEL_ERROR, "no such device %s\n", eth);
    return -1;
  }

  memcpy(&addr, &ifr.ifr_addr, sizeof(addr));
  strcpy(ipaddr, inet_ntoa(addr.sin_addr));
  close(sock_fd);

  return 0;
}

static int xy_network_4g_effectivenes(void)
{
  char ipaddr[32] = {0};

  xy_network_obtain("usb0", ipaddr);

  if (strlen(ipaddr) == 0) {
    ac_traces(MSG_LEVEL_INFO, "4G ip address is null");
    return false;
  }
  else {
    ac_traces(MSG_LEVEL_INFO, "4G ip address:%s", ipaddr);
    return true;
  }
}

static int xy_network_wireless_effectivenes(void)
{
  char ipaddr[32] = {0};

  xy_network_obtain("wlan0", ipaddr);

  if (strlen(ipaddr) == 0) {
    ac_traces(MSG_LEVEL_INFO, "wlan ip address is null");
    return false;
  }
  else {
    ac_traces(MSG_LEVEL_INFO, "wlan ip address:%s", ipaddr);
    return true;
  }
}

static void xy_network_led(void)
{
#if 0
  if (xy_network_4g_effectivenes() || xy_network_wireless_effectivenes())
#endif 
  if (xy_network_4g_effectivenes())
    xy_yellow_bright();
  else {
    xy_yellow_extinguish();
    ac_traces(MSG_LEVEL_INFO, "network invalid");
  }
}

void * xy_network_thread(void)
{
  while (b_quit == 0) {
    xy_network_led();
    sleep(1);
  }
}

int xy_network_thread_run(void)
{
  pthread_attr_t attr;
  pthread_t network_id = 0;
  int ret = 0;
  void * ptr;

  pthread_attr_init(&attr);

  if (0 != (ret = pthread_create(&network_id, &attr, (void * (*)(void *))xy_network_thread, NULL))) {
    ac_traces(MSG_LEVEL_ERROR, "create network led thread fail. error=%d", ret);
    return false;
  }

  ac_traces(MSG_LEVEL_INFO, "network led thread create successfuly!");

  pthread_join(network_id, &ptr);

  return true;
}
